var pager= 1,item_no='',item_id='',pdt_id='';
var productIds='';
var productQtys='';
var co_pro_id ='';
var co_pro_qty = '';
	$(function(){
	var product_info=JSON.parse(sessionStorage.getItem('product_info'));
	pdt_id=product_info.pdt_id;
	var pdt_item_no=product_info.pdt_item_no;
	
	var arr={
		//hf_s_id:"WD11",
		//hf_srv_id:"Pv_jpdt_get",
		//bs_item_no:pdt_item_no,
		//hs_srch_pdt_id:pdt_id,
		//hs_full_detail:"Y",
		//hs_action_id:"save"
		hf_s_id:"WD11",
		hf_srv_id:"Pv_jpdt_brw",
		hs_action_id:"submit",
		hs_rec_per_page:1,
		hs_srch_page_no:1,
		hs_full_detail:"Y",
		hs_pdt_id:pdt_id,
		hf_locale_id:"en",
	};
	getAjaxRequest(arr,function(result){
		console.log(result)
		var product=result.bvc_pdt[0];
		productIds = product.bj_pdt_id +'a';
		productQtys = product.bj_cart_qty+'a';
		co_pro_id = product.bj_pdt_id;
		co_pro_qty = product.bj_cart_qty;
		$('.detailImgBox .product_img').attr('src',MPJ.apiBase+''+product.bs_limg_file);
		$('.base-info').html('<h4 class="title">'+product.bs_brand+'</h4>'+
						'<p>'+product.bs_desc+'</p>'+
						'<div class="red-font">'+product.bs_prom_text+'</div>'+
						'<div class="price">$<em>'+getPrice(product.bj_rsp)+'</em></div>'+
						'<div class="operateBox">'+
							'<span>Quantity:</span>'+
								'<div class="mui-numbox" data-numbox-min="1" >'+
								'<button class="mui-btn mui-numbox-btn-minus" type="button">-</button>'+
								'<input class="mui-numbox-input" type="number" value="1" />'+
								'<button class="mui-btn mui-numbox-btn-plus" type="button">+</button>'+
							'</div>'+
						'<a href="javascript:void(0)" class="mui-btn addCar">'+
							'<img src="'+MPJ.base+'/resources/images/icon/main-shopcar.png" />Add to Cart'+
						'</a>'+
					'</div>');
		mui('.mui-numbox').numbox();
		collectionClick();

		var bvs_wd2_prom_text=product.bvs_wd2_prom_text;
		var bvs_promo_no=product.bvs_promo_no;
		if(bvs_wd2_prom_text.length != 0){
			var prom_text_str='';
			for (var i=0;i<bvs_wd2_prom_text.length;i++){
				if(i==0){
					prom_text_str+='<li><div class="mainCon active" data-item-no="'+bvs_promo_no[i]+'"><span>'+bvs_wd2_prom_text[i]+'</span></div></li>';
				}else{
					prom_text_str+='<li><div class="mainCon" data-item-no="'+bvs_promo_no[i]+'"><span>'+bvs_wd2_prom_text[i]+'</span></div></li>';
				}
			}
			$('.more-offer .slide-content ul').html(prom_text_str);

			moreOfferList();//more-offer
			item_no=$('.slide-content .mainCon').eq(0).data("item-no");
			prom_text=$('.slide-content .mainCon').eq(0).html();
			getPromotionList(bvs_promo_no[0],bvs_wd2_prom_text[0],1);//调用优惠列表方法
		}

		//点击添加到购物车
		$('.base-info .addCar').on('tap',function(){
			var arr={
				hf_s_id:"WD11",
				hf_srv_id:"Av_jcart",
				hs_action_id:"add_all_to_cart",
				hvs_add_pdt_id:product.bj_pdt_id,
				hvs_add_qty:$('.operateBox input').val(),
			};
			getAjaxRequest(arr,function(result){
				if(result.bs_result==BS_RESULT.OK){
					dialog('Message', 'Add to Shopping Cart', 1);
					var $dialog = $('#dialog');
					$dialog.show();
					$dialog.find('.comfirmBtn').one('click', function() {
						$dialog.hide();
						$dialog.remove();
						getCartSumCount();
					})
				}
			});
		})
	})

})

//回到頂部
$('.goTop').on('tap',function(){
	mui('#offCanvasContentScroll').scroll().scrollTo(0,0,100);
})

//点击清单按钮跳转到我的清单
$('.detailImgBox .order').on('tap',function(){
	//location.href=MPJ.base+"/pages/account/shopping/list.jsp";
	var product = productIds +'p'+productQtys;
	location.href=MPJ.base+"/pages/account/shopping/add.jsp?product="+product;
})



//more-offer
function moreOfferList(){
	var p=1;
	var $this=$('.more-offer .slideBox');
	var pLen=$this.find('li').length;
	var pWidth=$(window).width()-10;
	if(pWidth>630){
		pWidth=630;
	}
	var tWidth=pWidth*pLen/2;
	$this.find('.slide-content').css({width:tWidth});
	$this.find('ul').css({width:tWidth});
	$this.find('li').css({width:pWidth/2});

	$this.find('.controll-btn.next').on("tap",function(){
		p=p+1;
		if(p> Math.ceil(pLen/2)){
			p=1;
		}
		goPage(p);
	})
	$this.find('.controll-btn.prev').on("tap",function(){
		p=p-1;
		if(p<1){
			p= Math.ceil(pLen/2);
		}
		goPage(p);
	})
	function goPage(p){
		if(p<pLen){
			$('.slide-content').animate({marginLeft:-(pWidth*(p-1))+'px'});
		}else{
			p=1;
			$('.slide-content').animate({marginLeft:'0'});
		}
	}


	$('.slideBox .mainCon').on('tap',function(){
		$('.slideBox .mainCon').removeClass("active");
		$(this).addClass("active");

		item_no=$(this).data("item-no");
		prom_text=$(this).find('span').html();
		getPromotionList(item_no,prom_text,1);//调用优惠列表方法
	})
}


//调用优惠列表方法
function getPromotionList(item_no,prom_text,pager){
	$('.preferential p').html(prom_text);
	var arr={
		hs_promo_no:item_no,
		hs_pdt_id:pdt_id,
		hs_rec_per_page:[5,5,5,5,5],
		hs_srch_page_no:[1,1,1,1,1],
		hs_sort:'',
		hs_full_detail:"Y",
		hs_action_id:"submit",
		hf_s_id:"WD11",
		hf_srv_id:"Pv_jpromo_get",
		hs_call_ts:'',
		button:"submit",
		hf_locale_id:"en"
	};


	getAjaxRequest(arr,function(result){
		var productArr=setProductList_promotion(result);
		$('.more-offer .listContent.vertical-list ul').html(productArr[0]);
		if(productArr[1]==''){
			$('.redemption').hide();
		}else{
			$('.redemption .listContent.vertical-list ul').html(productArr[1]);
		}

		$('.listContent.vertical-list li').each(function(){
			var $this=$(this);
			$this.on('tap','.imgBox',function(){
				var pdt_id=$this.data('id');
				var item_no=$this.data('item-no');
				var product_info={};
				product_info.pdt_id=pdt_id;
				product_info.item_no=item_no;
				sessionStorage.setItem('product_info',JSON.stringify(product_info));
				location.href=MPJ.base+"/pages/product/product-detail.jsp";
			})
			$this.on('tap','.addCar',function(){
				var pdt_id=$this.data('id');
				var item_no=$this.data('item-no');
				var arr={
					hf_s_id:"WD11",
					hf_srv_id:"Av_jcart",
					hs_action_id:"add_all_to_cart",
					hvs_add_pdt_id:pdt_id,
					hvs_add_qty:$this.find('.mui-numbox-input').val(),
				};
				getAjaxRequest(arr,function(result){
					if(result.bs_result==BS_RESULT.OK){
						dialog('message', 'Add to Shopping Cart', 1);
						var $dialog = $('#dialog');
						$dialog.show();
						$dialog.find('.comfirmBtn').one('click', function() {
							$dialog.hide();
							$dialog.remove();
							getCartSumCount();
						})
					}

				});

			})
		})
	})

}


$('.more-offer-btn .mui-btn.mui-btn-block').on('tap',function(){
	pager++;
})


var collectionId = '';

function collection(collect,productId,productQty){
	var w=$(window).width();
	var arr = {
		hf_s_id:"WD11",
		hf_srv_id:"Cv_jcus_shoplist" ,
		hs_action_id: 'add_items_to_list',
		hs_list_id: collect,
		hvs_pdt_id: productId,
		hvs_pdt_qty: productQty
	}
	getAjaxRequest(arr,function(result){
		if(result.bs_result==BS_RESULT.OK){
			dialog('Message', 'Product has been successfully added to favorites', 4);
			var $dialog = $('#dialog');
			$dialog.show();
			$('.weui_dialog_ft').addClass('setWidth');
			if(w<375){
				$('.weui_dialog_ft a').css({'padding':'0 8px','margin':'0 5px'});
			}else{
				$('.weui_dialog_ft a').css({'padding':'0 30px','margin':'0 6px'});
			}
			$dialog.find('#giveBtn').one('click', function() {
				location.href = MPJ.base+"/pages/account/shopping/list.jsp";
				$dialog.hide();
				$dialog.remove();
			})
			$dialog.find('#comfirmBtn').one('click', function() {

				$dialog.hide();
				$dialog.remove();
			})
		}

	})
}
function typeList(){
	var arr = {
		hf_s_id: 'WD11',
		hf_srv_id: 'Cv_jcus_shoplist',
		hs_action_id: 'get_list',
		hs_list_id: "-1",
		myasync:true
	}
	getAjaxRequest(arr,function(result){
		collectionId = result[0].bs_id;
		collection(collectionId,co_pro_id,co_pro_qty);
	})
}
function collectionClick(){
	$('.collection').on('tap',function(){
		typeList();
	})
}
